#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#include "humanseg_c_wrap.h"


int main(int ac, char *av[])
{
 
    humanseg_ref_t h_seg;
    const char  *img_path;

    char *ptmp;
    int model_type;

    if (ac != 3){
        printf("usage : %s model_type img_path\n all model types: \n", av[0]);
        humanseg_print_all_models();
        return 0;
    }
 
    model_type = strtol(av[1], &ptmp, 10);


    img_path = av[2];

    h_seg = humanseg_create(model_type,  0, 1);

    if (!h_seg){
        printf("load models failed \n");
        return -1;
    }
    cv::Mat img = cv::imread(img_path);
    
    if (img.cols < 1 || img.rows < 1){
        printf("read image failed , path = %s\n", img_path);
        return -1;
    }
    cv::Mat img_rgb;

    cv::cvtColor(img, img_rgb, cv::COLOR_BGR2RGB);

    if (img.cols < 1){
        printf(" invalid image <%s>\n", img_path);
        return -1;

    }
    cv::Mat alpha(img_rgb.rows, img_rgb.cols, CV_8UC1);
    humanseg_detect_rgb24(h_seg, img_rgb.cols, img_rgb.rows, (unsigned char *)img_rgb.data, (int)img_rgb.step[0], (unsigned char *)alpha.data, (int)alpha.step[0]);

    cv::imshow("mask-out", alpha);
    cv::waitKey(0);
    cv::Mat img_bg = cv::imread("../data/bg0.jpg");
    cv::Mat img_bg1;
    cv::resize(img_bg, img_bg1, cv::Size(img_rgb.cols, img_rgb.rows));
    cv::Mat img_mix(img.rows, img.cols, CV_8UC3);

    humanseg_bg_replace_rgb24(h_seg, img.cols, img.rows, (unsigned char *)img.data, (int)img.step[0], 
        (unsigned char *)img_bg1.data, (int)img_bg1.step[0], img_mix.data, (int)img_mix.step[0],
         (unsigned char *)alpha.data, (int)alpha.step[0]
    );

    cv::imshow("mask-blend", img_mix);
    cv::waitKey(0);
 
    humanseg_destroy(h_seg);

    return 0;
}